/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dale2507.gates.data;

import dale2507.gates.GatePlugin;
import dale2507.gates.gate.logic.FailBeforeDial;
import dale2507.gates.gate.logic.IActivatorLogic;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;

/**
 *
 * @author Dale
 */
public class Settings {
    
    private static Settings instance;
    
    public static Settings getInstance() {
        if (instance == null) instance = new Settings();
        return instance;
    }
    
    private final FileConfiguration config;
    
    public GateCreationSettings creation = new GateCreationSettings();
    public GateConnectionSettings connection = new GateConnectionSettings();
    
    private Settings() {
        GatePlugin.getInstance().saveDefaultConfig();
        this.config = GatePlugin.getInstance().getConfig();
    }

    public void save() throws IOException {
        this.config.save(GatePlugin.getInstance().getDataFolder() + "/config.yml");
    }
    
    public class GateCreationSettings {
    
        private static final String PREFIX = "gate_creation.";
        
        private static final String GATE_MATERIAL = "gate_material";
        private static final String DEFAULT_CHEVRON = "default_chevron";
        private static final String SEARCH_AREA = "search_area";
        private static final String MINIMUM_CHEVRONS = "minimum_chevrons";
        private static final String OUTGOING_ONLY = "outgoing_only";
        private static final String AUTO_DIAL = "auto_dial";
        private static final String INDEFINATE_ITEM = "indefinite_item";
        private static final String FIX_ON_LOAD = "fix_on_load";
        
        public Material getGateMaterial() {
            return Material.getMaterial(config.getInt(PREFIX + GATE_MATERIAL));
        }
        
        public void setGateMaterial(Material m) {
            if (m.isBlock()) config.set(PREFIX + GATE_MATERIAL, m.getId());
        }

        public Material defaultChevronTorch() {
            int id = config.getInt(PREFIX + DEFAULT_CHEVRON);
            if (id != 50 && id != 76 && id != 0) {
                id = 76;
            }
            return Material.getMaterial(id);
        }
        
        public void setDefaultChevronTorch(Material m) {
            if (m == null) {
                config.set(PREFIX + DEFAULT_CHEVRON, 0);
            } else if (m.equals(Material.TORCH) || m.equals(Material.REDSTONE_TORCH_ON)) {
                config.set(PREFIX + DEFAULT_CHEVRON, m.getId());
            }
        }

        public Area searchArea() {
            return new Area(PREFIX + SEARCH_AREA);
        }
        
        public void setSearchArea(int horizontal, int vertical) {
            config.set(PREFIX + SEARCH_AREA + ".horizontal", horizontal);
            config.set(PREFIX + SEARCH_AREA + ".vertical", vertical);
        }
        
        public int minimumChevrons() {
            return config.getInt(PREFIX + MINIMUM_CHEVRONS);
        }
        
        public void setMinimumChevrons(int min) {
            if (min > 0 && min < 10) {
                config.set(PREFIX + MINIMUM_CHEVRONS, min);
            }
        }
        
        public boolean allowOutgoingOnly() {
            return config.getBoolean(PREFIX + OUTGOING_ONLY);
        }
        
        public void setOutgoingOnly(boolean outgoing) {
            config.set(PREFIX + OUTGOING_ONLY, outgoing);
        }
        
        public boolean allowAutoDial() {
            return config.getBoolean(PREFIX + AUTO_DIAL);
        }
        
        public void setAutoDial(boolean autoDial) {
            config.set(PREFIX + AUTO_DIAL, autoDial);
        }

        public Material indefiniteConnectionItem() {
            int id = config.getInt(PREFIX + INDEFINATE_ITEM);
            if (id == 0) return null;
            return Material.getMaterial(id);
        }
        
        public void setIndefiniteConnectionItem(Material m) {
            config.set(PREFIX + INDEFINATE_ITEM, m.getId());
        }
        
        public boolean fixOnLoad() {
            return config.getBoolean(PREFIX + FIX_ON_LOAD);
        }
        
        public void setFixOnLoad(boolean fix) {
            config.set(PREFIX + FIX_ON_LOAD, fix);
        }
        
    }
    
    public class GateConnectionSettings {
        
        private static final String PREFIX = "gate_connection.";
        
        private static final String ACTIVATOR_LOGIC = "activator_logic";
        private static final String CONNECTION_TIME = "connection_time";
        private static final String AUTO_OFF = "auto_off_time";
        private static final String CHEVRON_DELAY = "chevron_delay";
        private static final String CHECK_AMOUNT = "check_amount";
        private static final String VORTEX = "vortex";
        private static final String VEHICLES = "allow_vehicles";
        private static final String KILL_WRONG_WAY = "kill_wrong_way";
        private static final String IRIS_MATERIAL = "iris_material";
        
        public Class<? extends IActivatorLogic> activatorLogicClass() {
            try {
                String c = config.getString(PREFIX + ACTIVATOR_LOGIC);
                return (Class<? extends IActivatorLogic>) Class.forName("dale2507.gates.gate.logic." + c);
            } catch (ClassNotFoundException | ClassCastException ex) {
                Logger.getLogger(Settings.class.getName()).log(Level.WARNING, "Error getting activator. Returning default.", ex);
                return FailBeforeDial.class;
            }
        }
        
        public void setActivatorLogicClass(Class<? extends IActivatorLogic> logic) {
            config.set(PREFIX + ACTIVATOR_LOGIC, logic.getSimpleName());
        }

        public long maximumConnectionTime() {
            return config.getInt(PREFIX + CONNECTION_TIME) * 20L;
        }

        public void setConnectionTime(long l) {
            config.set(PREFIX + CONNECTION_TIME, l / 20L);
        }

        public long autoOffTime() {
            return config.getInt(PREFIX + AUTO_OFF) * 20L;
        }

        public void setAutoOff(long l) {
            config.set(PREFIX + AUTO_OFF, l / 20L);
        }

        public long chevronDelay() {
            return config.getLong(PREFIX + CHEVRON_DELAY);
        }

        public void setChevronDelay(long l) {
            config.set(PREFIX + CHEVRON_DELAY, l);
        }

        public boolean checkAmount() {
            return config.getBoolean(PREFIX + CHECK_AMOUNT);
        }

        public void setCheckAmount(boolean selected) {
            config.set(PREFIX + CHECK_AMOUNT, selected);
        }
        
        public boolean enableVortex() {
            return config.getBoolean(PREFIX + VORTEX);
        }

        public void setEnableVortex(boolean selected) {
            config.set(PREFIX + VORTEX, selected);
        }
        
        public boolean allowVehicles() {
            return config.getBoolean(PREFIX + VEHICLES);
        }

        public void setAllowVehicles(boolean selected) {
            config.set(PREFIX + VEHICLES, selected);
        }
        
        public boolean killOnWrongWay() {
            return config.getBoolean(PREFIX + KILL_WRONG_WAY);
        }

        public void setKillOnWrongWay(boolean selected) {
            config.set(PREFIX + KILL_WRONG_WAY, selected);
        }
        
        public Material irisMaterial() {
            return Material.getMaterial(config.getInt(PREFIX + IRIS_MATERIAL));
        }

        public void setIrisMaterial(Material m) {
            if (m.isBlock()) config.set(PREFIX + IRIS_MATERIAL, m.getId());
        }
        
    }
    
    public class Area {
        
        private String prefix;
        
        Area(String prefix) {
            this.prefix = prefix + ".";
        }
        
        public int getHorizontal() {
            return config.getInt(this.prefix + "horizontal");
        }
        
        public int getVertical() {
            return config.getInt(this.prefix + "vertical");
        }
        
    }
    
}
